<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>eventtarget.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_eventtarget.js.html">eventtarget.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implementation of EventTarget as defined by W3C DOM 2/3.
<a name="line17"></a> *
<a name="line18"></a> * @author arv@google.com (Erik Arvidsson) [Original implementation]
<a name="line19"></a> * @author pupius@google.com (Daniel Pupius) [Port to use goog.events]
<a name="line20"></a> * @see ../demos/eventtarget.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.events.EventTarget&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line26"></a>goog.require(&#39;goog.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.Listenable&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.Listener&#39;);
<a name="line30"></a>goog.require(&#39;goog.object&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Inherit from this class to give your object the ability to dispatch events.
<a name="line36"></a> * Note that this class provides event &lt;em&gt;sending&lt;/em&gt; behaviour, not event
<a name="line37"></a> * receiving behaviour: your object will be able to broadcast events, and other
<a name="line38"></a> * objects will be able to listen for those events using goog.events.listen().
<a name="line39"></a> *
<a name="line40"></a> * &lt;p&gt;The name &quot;EventTarget&quot; reflects the fact that this class implements the
<a name="line41"></a> * &lt;a href=&quot;http://www.w3.org/TR/DOM-Level-2-Events/events.html&quot;&gt;
<a name="line42"></a> * EventTarget interface&lt;/a&gt; as defined by W3C DOM 2/3, with a few differences:
<a name="line43"></a> * &lt;ul&gt;
<a name="line44"></a> * &lt;li&gt;Event objects do not have to implement the Event interface. An object
<a name="line45"></a> *     is treated as an event object if it has a &#39;type&#39; property.
<a name="line46"></a> * &lt;li&gt;You can use a plain string instead of an event object; an event-like
<a name="line47"></a> *     object will be created with the &#39;type&#39; set to the string value.
<a name="line48"></a> * &lt;/ul&gt;
<a name="line49"></a> *
<a name="line50"></a> * &lt;p&gt;Unless propagation is stopped, an event dispatched by an EventTarget
<a name="line51"></a> * will bubble to the parent returned by &lt;code&gt;getParentEventTarget&lt;/code&gt;.
<a name="line52"></a> * To set the parent, call &lt;code&gt;setParentEventTarget&lt;/code&gt; or override
<a name="line53"></a> * &lt;code&gt;getParentEventTarget&lt;/code&gt; in a subclass.  Subclasses that don&#39;t
<a name="line54"></a> * support changing the parent should override the setter to throw an error.
<a name="line55"></a> *
<a name="line56"></a> * &lt;p&gt;Example usage:
<a name="line57"></a> * &lt;pre&gt;
<a name="line58"></a> *   var source = new goog.events.EventTarget();
<a name="line59"></a> *   function handleEvent(event) {
<a name="line60"></a> *     alert(&#39;Type: &#39; + e.type + &#39;\nTarget: &#39; + e.target);
<a name="line61"></a> *   }
<a name="line62"></a> *   goog.events.listen(source, &#39;foo&#39;, handleEvent);
<a name="line63"></a> *   ...
<a name="line64"></a> *   source.dispatchEvent({type: &#39;foo&#39;}); // will call handleEvent
<a name="line65"></a> *   // or source.dispatchEvent(&#39;foo&#39;);
<a name="line66"></a> *   ...
<a name="line67"></a> *   goog.events.unlisten(source, &#39;foo&#39;, handleEvent);
<a name="line68"></a> *
<a name="line69"></a> *   // You can also use the Listener interface:
<a name="line70"></a> *   var listener = {
<a name="line71"></a> *     handleEvent: function(event) {
<a name="line72"></a> *       ...
<a name="line73"></a> *     }
<a name="line74"></a> *   };
<a name="line75"></a> *   goog.events.listen(source, &#39;bar&#39;, listener);
<a name="line76"></a> * &lt;/pre&gt;
<a name="line77"></a> *
<a name="line78"></a> * @constructor
<a name="line79"></a> * @extends {goog.Disposable}
<a name="line80"></a> * @implements {goog.events.Listenable}
<a name="line81"></a> */
<a name="line82"></a>goog.events.EventTarget = function() {
<a name="line83"></a>  goog.Disposable.call(this);
<a name="line84"></a>
<a name="line85"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line86"></a>    /**
<a name="line87"></a>     * Maps of event type to an array of listeners.
<a name="line88"></a>     *
<a name="line89"></a>     * @type {Object.&lt;string, !Array.&lt;!goog.events.ListenableKey&gt;&gt;}
<a name="line90"></a>     * @private
<a name="line91"></a>     */
<a name="line92"></a>    this.eventTargetListeners_ = {};
<a name="line93"></a>
<a name="line94"></a>    /**
<a name="line95"></a>     * Whether the EventTarget has been disposed. This is only true
<a name="line96"></a>     * when disposeInternal of EventTarget is completed (whereas
<a name="line97"></a>     * this.isDisposed() is true the moment obj.dispose() is called,
<a name="line98"></a>     * even before calling its disposeInternal).
<a name="line99"></a>     * @type {boolean}
<a name="line100"></a>     * @private
<a name="line101"></a>     */
<a name="line102"></a>    this.reallyDisposed_ = false;
<a name="line103"></a>
<a name="line104"></a>    /**
<a name="line105"></a>     * The object to use for event.target. Useful when mixing in an
<a name="line106"></a>     * EventTarget to another object.
<a name="line107"></a>     * @type {!Object}
<a name="line108"></a>     * @private
<a name="line109"></a>     */
<a name="line110"></a>    this.actualEventTarget_ = this;
<a name="line111"></a>  }
<a name="line112"></a>};
<a name="line113"></a>goog.inherits(goog.events.EventTarget, goog.Disposable);
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line117"></a>  goog.events.Listenable.addImplementation(goog.events.EventTarget);
<a name="line118"></a>}
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Used to tell if an event is a real event in goog.events.listen() so we don&#39;t
<a name="line123"></a> * get listen() calling addEventListener() and vice-versa.
<a name="line124"></a> * @type {boolean}
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.events.EventTarget.prototype.customEvent_ = true;
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Parent event target, used during event bubbling.
<a name="line132"></a> * @type {goog.events.EventTarget?}
<a name="line133"></a> * @private
<a name="line134"></a> */
<a name="line135"></a>goog.events.EventTarget.prototype.parentEventTarget_ = null;
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Returns the parent of this event target to use for bubbling.
<a name="line140"></a> *
<a name="line141"></a> * @return {goog.events.EventTarget} The parent EventTarget or null if there
<a name="line142"></a> * is no parent.
<a name="line143"></a> */
<a name="line144"></a>goog.events.EventTarget.prototype.getParentEventTarget = function() {
<a name="line145"></a>  return this.parentEventTarget_;
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Sets the parent of this event target to use for bubbling.
<a name="line151"></a> *
<a name="line152"></a> * @param {goog.events.EventTarget?} parent Parent EventTarget (null if none).
<a name="line153"></a> */
<a name="line154"></a>goog.events.EventTarget.prototype.setParentEventTarget = function(parent) {
<a name="line155"></a>  this.parentEventTarget_ = parent;
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Adds an event listener to the event target. The same handler can only be
<a name="line161"></a> * added once per the type. Even if you add the same handler multiple times
<a name="line162"></a> * using the same type then it will only be called once when the event is
<a name="line163"></a> * dispatched.
<a name="line164"></a> *
<a name="line165"></a> * Supported for legacy but use goog.events.listen(src, type, handler) instead.
<a name="line166"></a> *
<a name="line167"></a> * @param {string} type The type of the event to listen for.
<a name="line168"></a> * @param {Function|Object} handler The function to handle the event. The
<a name="line169"></a> *     handler can also be an object that implements the handleEvent method
<a name="line170"></a> *     which takes the event object as argument.
<a name="line171"></a> * @param {boolean=} opt_capture In DOM-compliant browsers, this determines
<a name="line172"></a> *     whether the listener is fired during the capture or bubble phase
<a name="line173"></a> *     of the event.
<a name="line174"></a> * @param {Object=} opt_handlerScope Object in whose scope to call
<a name="line175"></a> *     the listener.
<a name="line176"></a> */
<a name="line177"></a>goog.events.EventTarget.prototype.addEventListener = function(
<a name="line178"></a>    type, handler, opt_capture, opt_handlerScope) {
<a name="line179"></a>  goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Removes an event listener from the event target. The handler must be the
<a name="line185"></a> * same object as the one added. If the handler has not been added then
<a name="line186"></a> * nothing is done.
<a name="line187"></a> * @param {string} type The type of the event to listen for.
<a name="line188"></a> * @param {Function|Object} handler The function to handle the event. The
<a name="line189"></a> *     handler can also be an object that implements the handleEvent method
<a name="line190"></a> *     which takes the event object as argument.
<a name="line191"></a> * @param {boolean=} opt_capture In DOM-compliant browsers, this determines
<a name="line192"></a> *     whether the listener is fired during the capture or bubble phase
<a name="line193"></a> *     of the event.
<a name="line194"></a> * @param {Object=} opt_handlerScope Object in whose scope to call
<a name="line195"></a> *     the listener.
<a name="line196"></a> */
<a name="line197"></a>goog.events.EventTarget.prototype.removeEventListener = function(
<a name="line198"></a>    type, handler, opt_capture, opt_handlerScope) {
<a name="line199"></a>  goog.events.unlisten(this, type, handler, opt_capture, opt_handlerScope);
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/** @override */
<a name="line204"></a>goog.events.EventTarget.prototype.dispatchEvent = function(e) {
<a name="line205"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line206"></a>    if (this.reallyDisposed_) {
<a name="line207"></a>      return true;
<a name="line208"></a>    }
<a name="line209"></a>
<a name="line210"></a>    var ancestorsTree, ancestor = this.getParentEventTarget();
<a name="line211"></a>    if (ancestor) {
<a name="line212"></a>      ancestorsTree = [];
<a name="line213"></a>      for (; ancestor; ancestor = ancestor.getParentEventTarget()) {
<a name="line214"></a>        ancestorsTree.push(ancestor);
<a name="line215"></a>      }
<a name="line216"></a>    }
<a name="line217"></a>
<a name="line218"></a>    return goog.events.EventTarget.dispatchEventInternal_(
<a name="line219"></a>        this.actualEventTarget_, e, ancestorsTree);
<a name="line220"></a>  } else {
<a name="line221"></a>    return goog.events.dispatchEvent(this, e);
<a name="line222"></a>  }
<a name="line223"></a>};
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * Unattach listeners from this object.  Classes that extend EventTarget may
<a name="line228"></a> * need to override this method in order to remove references to DOM Elements
<a name="line229"></a> * and additional listeners, it should be something like this:
<a name="line230"></a> * &lt;pre&gt;
<a name="line231"></a> * MyClass.prototype.disposeInternal = function() {
<a name="line232"></a> *   MyClass.superClass_.disposeInternal.call(this);
<a name="line233"></a> *   // Dispose logic for MyClass
<a name="line234"></a> * };
<a name="line235"></a> * &lt;/pre&gt;
<a name="line236"></a> * @override
<a name="line237"></a> * @protected
<a name="line238"></a> */
<a name="line239"></a>goog.events.EventTarget.prototype.disposeInternal = function() {
<a name="line240"></a>  goog.events.EventTarget.superClass_.disposeInternal.call(this);
<a name="line241"></a>
<a name="line242"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line243"></a>    this.removeAllListeners();
<a name="line244"></a>    this.reallyDisposed_ = true;
<a name="line245"></a>  } else {
<a name="line246"></a>    goog.events.removeAll(this);
<a name="line247"></a>  }
<a name="line248"></a>
<a name="line249"></a>  this.parentEventTarget_ = null;
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line254"></a>
<a name="line255"></a>/** @override */
<a name="line256"></a>goog.events.EventTarget.prototype.listen = function(
<a name="line257"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line258"></a>  return this.listenInternal_(
<a name="line259"></a>      type, listener, false /* callOnce */, opt_useCapture, opt_listenerScope);
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/** @override */
<a name="line264"></a>goog.events.EventTarget.prototype.listenOnce = function(
<a name="line265"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line266"></a>  return this.listenInternal_(
<a name="line267"></a>      type, listener, true /* callOnce */, opt_useCapture, opt_listenerScope);
<a name="line268"></a>};
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/**
<a name="line272"></a> * Adds an event listener. A listener can only be added once to an
<a name="line273"></a> * object and if it is added again the key for the listener is
<a name="line274"></a> * returned.
<a name="line275"></a> *
<a name="line276"></a> * Note that a one-off listener will not change an existing listener,
<a name="line277"></a> * if any. On the other hand a normal listener will change existing
<a name="line278"></a> * one-off listener to become a normal listener.
<a name="line279"></a> *
<a name="line280"></a> * @param {string} type Event type to listen to.
<a name="line281"></a> * @param {!Function} listener Callback method.
<a name="line282"></a> * @param {boolean} callOnce Whether the listener is a one-off
<a name="line283"></a> *     listener or otherwise.
<a name="line284"></a> * @param {boolean=} opt_useCapture Whether to fire in capture phase
<a name="line285"></a> *     (defaults to false).
<a name="line286"></a> * @param {Object=} opt_listenerScope Object in whose scope to call the
<a name="line287"></a> *     listener.
<a name="line288"></a> * @return {goog.events.ListenableKey} Unique key for the listener.
<a name="line289"></a> * @private
<a name="line290"></a> */
<a name="line291"></a>goog.events.EventTarget.prototype.listenInternal_ = function(
<a name="line292"></a>    type, listener, callOnce, opt_useCapture, opt_listenerScope) {
<a name="line293"></a>  goog.asserts.assert(
<a name="line294"></a>      !this.reallyDisposed_, &#39;Can not listen on disposed object.&#39;);
<a name="line295"></a>
<a name="line296"></a>  var listenerArray = this.eventTargetListeners_[type] ||
<a name="line297"></a>      (this.eventTargetListeners_[type] = []);
<a name="line298"></a>
<a name="line299"></a>  var listenerObj;
<a name="line300"></a>  var index = goog.events.EventTarget.findListenerIndex_(
<a name="line301"></a>      listenerArray, listener, opt_useCapture, opt_listenerScope);
<a name="line302"></a>  if (index &gt; -1) {
<a name="line303"></a>    listenerObj = listenerArray[index];
<a name="line304"></a>    if (!callOnce) {
<a name="line305"></a>      // Ensure that, if there is an existing callOnce listener, it is no
<a name="line306"></a>      // longer a callOnce listener.
<a name="line307"></a>      listenerObj.callOnce = false;
<a name="line308"></a>    }
<a name="line309"></a>    return listenerObj;
<a name="line310"></a>  }
<a name="line311"></a>
<a name="line312"></a>  listenerObj = new goog.events.Listener();
<a name="line313"></a>  listenerObj.init(
<a name="line314"></a>      listener, null, this, type, !!opt_useCapture, opt_listenerScope);
<a name="line315"></a>  listenerObj.callOnce = callOnce;
<a name="line316"></a>  listenerArray.push(listenerObj);
<a name="line317"></a>
<a name="line318"></a>  return listenerObj;
<a name="line319"></a>};
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/** @override */
<a name="line323"></a>goog.events.EventTarget.prototype.unlisten = function(
<a name="line324"></a>    type, listener, opt_useCapture, opt_listenerScope) {
<a name="line325"></a>  if (!(type in this.eventTargetListeners_)) {
<a name="line326"></a>    return false;
<a name="line327"></a>  }
<a name="line328"></a>
<a name="line329"></a>  var listenerArray = this.eventTargetListeners_[type];
<a name="line330"></a>  var index = goog.events.EventTarget.findListenerIndex_(
<a name="line331"></a>      listenerArray, listener, opt_useCapture, opt_listenerScope);
<a name="line332"></a>  if (index &gt; -1) {
<a name="line333"></a>    var listenerObj = listenerArray[index];
<a name="line334"></a>    goog.events.cleanUp(listenerObj);
<a name="line335"></a>    listenerObj.removed = true;
<a name="line336"></a>    return goog.array.removeAt(listenerArray, index);
<a name="line337"></a>  }
<a name="line338"></a>  return false;
<a name="line339"></a>};
<a name="line340"></a>
<a name="line341"></a>
<a name="line342"></a>/** @override */
<a name="line343"></a>goog.events.EventTarget.prototype.unlistenByKey = function(key) {
<a name="line344"></a>  var type = key.type;
<a name="line345"></a>  if (!(type in this.eventTargetListeners_)) {
<a name="line346"></a>    return false;
<a name="line347"></a>  }
<a name="line348"></a>
<a name="line349"></a>  var removed = goog.array.remove(this.eventTargetListeners_[type], key);
<a name="line350"></a>  if (removed) {
<a name="line351"></a>    goog.events.cleanUp(key);
<a name="line352"></a>    key.removed = true;
<a name="line353"></a>  }
<a name="line354"></a>  return removed;
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/** @override */
<a name="line359"></a>goog.events.EventTarget.prototype.removeAllListeners = function(
<a name="line360"></a>    opt_type, opt_capture) {
<a name="line361"></a>  var count = 0;
<a name="line362"></a>  for (var type in this.eventTargetListeners_) {
<a name="line363"></a>    if (!opt_type || type == opt_type) {
<a name="line364"></a>      var listenerArray = this.eventTargetListeners_[type];
<a name="line365"></a>      for (var i = 0; i &lt; listenerArray.length; i++) {
<a name="line366"></a>        ++count;
<a name="line367"></a>        goog.events.cleanUp(listenerArray[i]);
<a name="line368"></a>        listenerArray[i].removed = true;
<a name="line369"></a>      }
<a name="line370"></a>      listenerArray.length = 0;
<a name="line371"></a>    }
<a name="line372"></a>  }
<a name="line373"></a>  return count;
<a name="line374"></a>};
<a name="line375"></a>
<a name="line376"></a>
<a name="line377"></a>/** @override */
<a name="line378"></a>goog.events.EventTarget.prototype.fireListeners = function(
<a name="line379"></a>    type, capture, eventObject) {
<a name="line380"></a>  goog.asserts.assert(
<a name="line381"></a>      !this.reallyDisposed_,
<a name="line382"></a>      &#39;Can not fire listeners after dispose() completed.&#39;);
<a name="line383"></a>
<a name="line384"></a>  if (!(type in this.eventTargetListeners_)) {
<a name="line385"></a>    return true;
<a name="line386"></a>  }
<a name="line387"></a>
<a name="line388"></a>  var rv = true;
<a name="line389"></a>  var listenerArray = goog.array.clone(this.eventTargetListeners_[type]);
<a name="line390"></a>  for (var i = 0; i &lt; listenerArray.length; ++i) {
<a name="line391"></a>    var listener = listenerArray[i];
<a name="line392"></a>    // We might not have a listener if the listener was removed.
<a name="line393"></a>    if (listener &amp;&amp; !listener.removed &amp;&amp; listener.capture == capture) {
<a name="line394"></a>      // TODO(user): This logic probably should be in the Listener
<a name="line395"></a>      // object instead.
<a name="line396"></a>      if (listener.callOnce) {
<a name="line397"></a>        this.unlistenByKey(listener);
<a name="line398"></a>      }
<a name="line399"></a>      rv = listener.handleEvent(eventObject) !== false &amp;&amp; rv;
<a name="line400"></a>    }
<a name="line401"></a>  }
<a name="line402"></a>
<a name="line403"></a>  return rv &amp;&amp; eventObject.returnValue_ != false;
<a name="line404"></a>};
<a name="line405"></a>
<a name="line406"></a>
<a name="line407"></a>/** @override */
<a name="line408"></a>goog.events.EventTarget.prototype.getListeners = function(type, capture) {
<a name="line409"></a>  var listenerArray = this.eventTargetListeners_[type];
<a name="line410"></a>  var rv = [];
<a name="line411"></a>  if (listenerArray) {
<a name="line412"></a>    for (var i = 0; i &lt; listenerArray.length; ++i) {
<a name="line413"></a>      var listenerObj = listenerArray[i];
<a name="line414"></a>      if (listenerObj.capture == capture) {
<a name="line415"></a>        rv.push(listenerObj);
<a name="line416"></a>      }
<a name="line417"></a>    }
<a name="line418"></a>  }
<a name="line419"></a>  return rv;
<a name="line420"></a>};
<a name="line421"></a>
<a name="line422"></a>
<a name="line423"></a>/** @override */
<a name="line424"></a>goog.events.EventTarget.prototype.getListener = function(
<a name="line425"></a>    type, listener, capture, opt_listenerScope) {
<a name="line426"></a>  var listenerArray = this.eventTargetListeners_[type];
<a name="line427"></a>  var i = -1;
<a name="line428"></a>  if (listenerArray) {
<a name="line429"></a>    i = goog.events.EventTarget.findListenerIndex_(
<a name="line430"></a>        listenerArray, listener, capture, opt_listenerScope);
<a name="line431"></a>  }
<a name="line432"></a>  return i &gt; -1 ? listenerArray[i] : null;
<a name="line433"></a>};
<a name="line434"></a>
<a name="line435"></a>
<a name="line436"></a>/** @override */
<a name="line437"></a>goog.events.EventTarget.prototype.hasListener = function(
<a name="line438"></a>    opt_type, opt_capture) {
<a name="line439"></a>  var hasType = goog.isDef(opt_type);
<a name="line440"></a>  var hasCapture = goog.isDef(opt_capture);
<a name="line441"></a>
<a name="line442"></a>  return goog.object.some(
<a name="line443"></a>      this.eventTargetListeners_, function(listenersArray, type) {
<a name="line444"></a>        for (var i = 0; i &lt; listenersArray.length; ++i) {
<a name="line445"></a>          if ((!hasType || listenersArray[i].type == opt_type) &amp;&amp;
<a name="line446"></a>              (!hasCapture || listenersArray[i].capture == opt_capture)) {
<a name="line447"></a>            return true;
<a name="line448"></a>          }
<a name="line449"></a>        }
<a name="line450"></a>
<a name="line451"></a>        return false;
<a name="line452"></a>      });
<a name="line453"></a>};
<a name="line454"></a>
<a name="line455"></a>
<a name="line456"></a>/**
<a name="line457"></a> * Sets the target to be used for {@code event.target} when firing
<a name="line458"></a> * event. Mainly used for testing. For example, see
<a name="line459"></a> * {@code goog.testing.events.mixinListenable}.
<a name="line460"></a> * @param {!Object} target The target.
<a name="line461"></a> */
<a name="line462"></a>goog.events.EventTarget.prototype.setTargetForTesting = function(target) {
<a name="line463"></a>  this.actualEventTarget_ = target;
<a name="line464"></a>};
<a name="line465"></a>
<a name="line466"></a>
<a name="line467"></a>/**
<a name="line468"></a> * Dispatches the given event on the ancestorsTree.
<a name="line469"></a> *
<a name="line470"></a> * TODO(user): Look for a way to reuse this logic in
<a name="line471"></a> * goog.events, if possible.
<a name="line472"></a> *
<a name="line473"></a> * @param {!Object} target The target to dispatch on.
<a name="line474"></a> * @param {goog.events.Event|Object|string} e The event object.
<a name="line475"></a> * @param {Array.&lt;goog.events.Listenable&gt;=} opt_ancestorsTree The ancestors
<a name="line476"></a> *     tree of the target, in reverse order from the closest ancestor
<a name="line477"></a> *     to the root event target. May be null if the target has no ancestor.
<a name="line478"></a> * @return {boolean} If anyone called preventDefault on the event object (or
<a name="line479"></a> *     if any of the listeners returns false this will also return false.
<a name="line480"></a> * @private
<a name="line481"></a> */
<a name="line482"></a>goog.events.EventTarget.dispatchEventInternal_ = function(
<a name="line483"></a>    target, e, opt_ancestorsTree) {
<a name="line484"></a>  var type = e.type || /** @type {string} */ (e);
<a name="line485"></a>
<a name="line486"></a>  // If accepting a string or object, create a custom event object so that
<a name="line487"></a>  // preventDefault and stopPropagation work with the event.
<a name="line488"></a>  if (goog.isString(e)) {
<a name="line489"></a>    e = new goog.events.Event(e, target);
<a name="line490"></a>  } else if (!(e instanceof goog.events.Event)) {
<a name="line491"></a>    var oldEvent = e;
<a name="line492"></a>    e = new goog.events.Event(type, target);
<a name="line493"></a>    goog.object.extend(e, oldEvent);
<a name="line494"></a>  } else {
<a name="line495"></a>    e.target = e.target || target;
<a name="line496"></a>  }
<a name="line497"></a>
<a name="line498"></a>  var rv = true, currentTarget;
<a name="line499"></a>
<a name="line500"></a>  // Executes all capture listeners on the ancestors, if any.
<a name="line501"></a>  if (opt_ancestorsTree) {
<a name="line502"></a>    for (var i = opt_ancestorsTree.length - 1; !e.propagationStopped_ &amp;&amp; i &gt;= 0;
<a name="line503"></a>         i--) {
<a name="line504"></a>      currentTarget = e.currentTarget = opt_ancestorsTree[i];
<a name="line505"></a>      rv = currentTarget.fireListeners(type, true, e) &amp;&amp; rv;
<a name="line506"></a>    }
<a name="line507"></a>  }
<a name="line508"></a>
<a name="line509"></a>  // Executes capture and bubble listeners on the target.
<a name="line510"></a>  if (!e.propagationStopped_) {
<a name="line511"></a>    currentTarget = e.currentTarget = target;
<a name="line512"></a>    rv = currentTarget.fireListeners(type, true, e) &amp;&amp; rv;
<a name="line513"></a>    if (!e.propagationStopped_) {
<a name="line514"></a>      rv = currentTarget.fireListeners(type, false, e) &amp;&amp; rv;
<a name="line515"></a>    }
<a name="line516"></a>  }
<a name="line517"></a>
<a name="line518"></a>  // Executes all bubble listeners on the ancestors, if any.
<a name="line519"></a>  if (opt_ancestorsTree) {
<a name="line520"></a>    for (i = 0; !e.propagationStopped_ &amp;&amp; i &lt; opt_ancestorsTree.length; i++) {
<a name="line521"></a>      currentTarget = e.currentTarget = opt_ancestorsTree[i];
<a name="line522"></a>      rv = currentTarget.fireListeners(type, false, e) &amp;&amp; rv;
<a name="line523"></a>    }
<a name="line524"></a>  }
<a name="line525"></a>
<a name="line526"></a>  return rv;
<a name="line527"></a>};
<a name="line528"></a>
<a name="line529"></a>
<a name="line530"></a>/**
<a name="line531"></a> * Finds the index of a matching goog.events.Listener in the given
<a name="line532"></a> * listenerArray.
<a name="line533"></a> * @param {!Array.&lt;!goog.events.Listener&gt;} listenerArray Array of listener.
<a name="line534"></a> * @param {!Function} listener The listener function.
<a name="line535"></a> * @param {boolean=} opt_useCapture The capture flag for the listener.
<a name="line536"></a> * @param {Object=} opt_listenerScope The listener scope.
<a name="line537"></a> * @return {number} The index of the matching listener within the
<a name="line538"></a> *     listenerArray.
<a name="line539"></a> * @private
<a name="line540"></a> */
<a name="line541"></a>goog.events.EventTarget.findListenerIndex_ = function(
<a name="line542"></a>    listenerArray, listener, opt_useCapture, opt_listenerScope) {
<a name="line543"></a>  for (var i = 0; i &lt; listenerArray.length; ++i) {
<a name="line544"></a>    var listenerObj = listenerArray[i];
<a name="line545"></a>    if (listenerObj.listener == listener &amp;&amp;
<a name="line546"></a>        listenerObj.capture == !!opt_useCapture &amp;&amp;
<a name="line547"></a>        listenerObj.handler == opt_listenerScope) {
<a name="line548"></a>      return i;
<a name="line549"></a>    }
<a name="line550"></a>  }
<a name="line551"></a>  return -1;
<a name="line552"></a>};
<a name="line553"></a>
<a name="line554"></a>}  // if (goog.events.Listenable.USE_LISTENABLE_INTERFACE)
</pre>


</body>
</html>
